package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;

import entity.Admin;

public class AdminDAO {
    
    /**
     * Load Admin in Shop.
     * @return List Admin
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public static ArrayList<Admin> getList() throws Exception {
        ArrayList<Admin> list = new ArrayList<Admin>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Admin>) session.createCriteria(Admin.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Admin
     * @param Admin
     * @throws Exception
     */
    public static void add(Admin admin) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(admin);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Admin
     * @param Admin
     * @throws Exception
     */
    public static void update(Admin admin) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(admin);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Admin
     * @param Admin
     * @throws Exception
     */
    public static void delete(Admin admin) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(admin);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Admin by ID
     * @param id
     * @return Admin
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Admin find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Admin admin = new Admin();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            admin = (Admin) session.get(Admin.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return admin;
        }
    }

}
